﻿using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using RIA.ServicesLibrary.Web.Models;
using SL4.CustomControls;
namespace RIA.NTiersApp.Globals
{
    public class ListCaches
    {
        public static CustomPagedCollectionView<ProductModel> ProductModels { get; set; }
        public static CustomPagedCollectionView<ProductCategory> ProductCategories { get; set; }

         static ListCaches ()
        {
            ProductModels = new CustomPagedCollectionView<ProductModel> ();
            ProductCategories = new CustomPagedCollectionView<ProductCategory> ();
        }
    }


    public class ListCacheLoader:ViewModel.ViewModelBase
    {

        RIA.ServicesLibrary.Web.Services.ProductContext _productContext = null;
        #region ProductContext
        private RIA.ServicesLibrary.Web.Services.ProductContext ProductContext
        {
            get
            {
                if ( _productContext == null )
                {
                    _productContext = new ServicesLibrary.Web.Services.ProductContext ();
                }
                return _productContext;
            }
        }
        #endregion

        static Dictionary<string, bool> _LoadedFlags = new Dictionary<string, bool> ();
        public ListCacheLoader ()
        {
            _LoadedFlags.Clear ();
            _LoadedFlags.Add ( "ProductModels", false );
            _LoadedFlags.Add ( "ProductCategories", false );
        }
        

        private void LoadProductModels ()
        {
            EntityQuery<ProductModel> qry = this.ProductContext.GetProductModelsQuery ();
            LoadOperation<ProductModel> op = this.ProductContext.Load ( qry, true );
            op.Completed += ( x, y ) =>
            {
               
                if ( op.HasError )
                {
                    System.Windows.MessageBox.Show ( op.Error.ToString (), "Load Error",
                    System.Windows.MessageBoxButton.OK );
                    op.MarkErrorAsHandled ();
                }
                else
                {
                    ListCaches.ProductModels.Clear ();
                    foreach ( ProductModel c in op.Entities )
                    {
                        ListCaches.ProductModels.Add ( c );
                    }
                    _LoadedFlags [ "ProductModels" ] = true;
                    MonitorIfLoading ();
                }

            };
        }

       // public  static CustomPagedCollectionView<ProductCategory> ProductCategories { get; set; }

        private void LoadProductCategories ()
        {
            EntityQuery<ProductCategory> qry = this.ProductContext.GetProductCategoriesQuery ();
            LoadOperation<ProductCategory> op = this.ProductContext.Load ( qry, true );
            op.Completed += ( x, y ) =>
            {
                
                if ( op.HasError )
                {
                    System.Windows.MessageBox.Show ( op.Error.ToString (), "Load Error",
                    System.Windows.MessageBoxButton.OK );
                    op.MarkErrorAsHandled ();
                }
                else
                {
                    ListCaches.ProductCategories.Clear ();
                    foreach ( ProductCategory c in op.Entities )
                    {
                        ListCaches.ProductCategories.Add ( c );
                    }
                    
                    _LoadedFlags [ "ProductCategories" ] = true;
                    MonitorIfLoading ();
                }

            };
        }

       

        public void LoadAllList ()
        {
            ShowBusyStatus ();
            LoadProductModels ();
            LoadProductCategories ();
           
            
        }

        #region IsLoading
        private void MonitorIfLoading ()
        {
            lock ( this )
            {
                bool isLoading = false;
                foreach ( bool isItemLoaded in _LoadedFlags.Values )
                {
                    if ( isItemLoaded == false )
                    {
                        isLoading = true;
                    }
                }
                if ( !isLoading )
                {
                    this.HideBusyStatus ();
                }
            }
            
        }

       

        #endregion

       

        


        
    }
}
